/*
   Move comments form
*/

var slider_auto = 0;

function move_form_to(ee)
{
   var e = $("#form_holder").html();
   var tt = $("#form_holder .share_comment").text();
   $("#form_holder").slideUp(500, function () {
      $("#form_holder").remove();
      
      ee.append('<div id="form_holder">'+e+'</div>');
      $("#form_holder .share_comment").html(tt);
      $("#form_holder [valed]").removeAttr('valed');
      $("#form_holder .do_clear").attr('remove', 1);
      
      Cufon('#form_holder .share_comment', {
         color: '-linear-gradient(#b8b4b1, #edebe8)', textShadow: '1px 1px #000'
      });
      
      $(".formError").remove();
      
      $("#form_holder").hide().slideDown(500);
            
      if (ee.parent().attr("id") != "form_prev_holder")
         {
            var eid = ee.parent().attr("id");
            if (!eid)
               eid = "";
            $("#comment_parent").val( eid.replace('comment-', '') );
         }
         else
         {
            $("#comment_parent").val("0");
         }
      
      upd_ev();
   });
}

function upd_ev()
{
   /* 
      Forms validation
   */
   
   $("[placeholder]").each(function () {
      $(this).val( $(this).val().replace( $(this).attr("placeholder"), "" ) );
      $(this).placeholder();
   });
   
   $("form .go_submit").unbind().click(function () {
      var e=$(this).parents("form");
      e.find("input, textarea").each(function () {
         $(this).unbind();
         $(this).val( $(this).val().replace( $(this).attr("placeholder"), "" ) );
      });
      if ( !e.attr("valed") && e.hasClass('ajaxing') ) {
        e.validationEngine({
            ajaxSubmit: true,
            ajaxSubmitFile: e.attr("action")
        });
      }else if( !e.attr("valed") ) {
        e.validationEngine();
      }
      e.attr("valed", "1");
      e.submit(); 
      e.find("input, textarea").each(function () {
         $(this).placeholder();
      });      
      return false;
   });
   $("form .do_clear").unbind().click(function () {
      $(this).parents("form").find("input, textarea").each(function () {
         $(this).val("").placeholder();
      });
      $(".formError").remove();
      
      if ($(this).attr("remove") && !$(this).parents("#form_prev_holder").length) 
      {
         move_form_to( $("#form_prev_holder") );
         $("#form_holder .do_clear").removeAttr('remove');
      }
      
      return false;
   });
   
   /*
      End: Forms validation
   */
}

/* 
    Fade for gallery and portfolio
*/ 
function dt_gal_portf_fade() {
    var blocks_speed_fade_in  = 400;
    var blocks_speed_fade_out = 400;
   
    $(".col_1-3 .desc").css({
        display: 'block',
        opacity: 0
    });
 
    $(".col_1-3").hover(function () {
        $(".desc", this).animate({
            opacity: 1
        }, {
            duration:   blocks_speed_fade_in,
            queue:      false,
            complete:   function () {
                if ($.browser.msie) this.style.removeAttribute('filter');
            }
        });
    }, function () {
        $(".desc", this).animate({
            opacity:    0
        }, {
            duration:   blocks_speed_fade_out,
            queue:      false
        });
    });
}
/*
fade end
*/

/*
photos fade script begin
*/

function dt_photos_fade( selector ) {
    $( selector ).append('<span class="fadeMe"></span>').each(function () {
      var $span = $('span', this);
     if ($.browser.msie && $.browser.version < 9)
        $span.hide();
     else
        $span.css('opacity', 0);
      $(this).hover(function () {
        if ($.browser.msie && $.browser.version < 9)
          $span.show();
        else
        $span.stop().fadeTo(400, 1);
      }, function () {
        if ($.browser.msie && $.browser.version < 9)
          $span.hide();
        else
          $span.stop().fadeTo(400, 0);
      });
    });
    $(".col_1-3, .news_ico").each(function () {
        var i=$(this).find("img");
        var i_w = i.attr('width');
		var i_h = i.attr('height');
        $('.loading-image', this).css('width', i_w);
        $('.loading-image', this).css('height', i_h);
        $('span.fadeMe', this).css('width', i_w);
        $('span.fadeMe', this).css('height', i_h);
    });
	

	$("a.blog_ico").each(function () {
		var i=$(this).find(" > img");
		var i_w = i.attr('width');
		var i_h = i.attr('height');
		$('span', this).css('width', i_w);
		$('span', this).css('height', i_h);
	});


	$("ul.gallery li").each(function () {
		var i=$(this).find("img");
		var i_w = i.attr('width');
		var i_h = i.attr('height');
		$('span', this).css('width', i_w);
		$('span', this).css('height', i_h);
	});

}

/*
photos fade end
*/

$(document).ready(function () {

   $(".comment .comments_count").click(function () {
      move_form_to( $(this).parent().parent() );
      return false;
   });

   /* Thumbnails */

   var popup_options = {
      jump_height:        30,
      show_duration:      300,
      hide_duration:      300,
      tout:               200,
      top:                -131
   };
   
   if (!$.browser.msie)
   {
      $("ul.pxs_thumbnails li div").css({
         display:       'none',
         opacity:       0
      });
   }
   
   var touts    = new Array();
   var cur_elem = null;
   var n=0;
   $("ul.pxs_thumbnails li").each(function () {
      $(this).attr("n", ++n);
   });
   
   $("ul.pxs_thumbnails li").hover(function () {
      cur_elem = $("div", this);
      touts[ parseInt($(this).attr("n")) ] = setTimeout(function () {
         if ($.browser.msie)
         {
            cur_elem.show().css({
               top:        popup_options.top+"px"
            });
         }
         else
         {
            cur_elem.show().css({
               opacity:    0,
               top: ( popup_options.top - popup_options.jump_height )+"px"
            }).animate({
               opacity:    1,
               top:        popup_options.top+"px"
            }, {
               duration:   popup_options.show_duration,
               queue:      false,
               complete:   function () {
                  //$(this).hide();
               }
            });
         }
      }, popup_options.tout);
   }, function () {
   
      if (touts[ parseInt($(this).attr("n")) ]) clearTimeout(touts[ parseInt($(this).attr("n")) ]);
   
      if ($.browser.msie)
      {
         $("div", this).hide();
      }
      else
      {
         $("div", this).animate({
            opacity:    0
         }, {
            duration:   popup_options.hide_duration,
            queue:      false,
            complete:   function () {
               $(this).hide();
            }
         });
      }   
   });
   
   /* End: Thumbnails */


   /* Slider Extension */
   
   if ($('#pxs_container').length)
   {

      var arrow_duration  = 300;
      var arrow_prev      = $( $(".pxs_slider").children()[0] );
      var arrow_left_init = "-"+$("#pxs_container div.desc").width() +"px";
	  
	  function isiPad(){
		//return 1;
		return (navigator.platform.indexOf("iPad") != -1);
	}
 
      $("#pxs_container div.desc:gt(0)").css({ 
         left: arrow_left_init
      });

	   $('#pxs_container').parallaxSlider({
	      auto: slider_auto,
	      animDone: function (parent) {

				var t = 197 - $(".desc_outer", parent).height()/2;
				$("div.desc_outer", parent).css({ 
				  position:'absolute', 
				  top: t+"px"
				});
 
				$("div.desc_outer", parent).addClass("posdone");

	         $("div.desc", parent).show().animate({
	            left: '0px'
	         }, {
	            duration: arrow_duration,
	            queue: false, 
	            complete: function () {
	               $("div.desc", arrow_prev).css('left', arrow_left_init);
	               arrow_prev = parent;
	            }
	         });
		  }
		});
	}

   /* End: Slider Extension */
   
   /* 
      Fade for gallery and portfolio
   */   
   
   dt_gal_portf_fade();
   
   /*
      End: Fade for gallery and portfolio 
   */

   upd_ev();

/*   
   if (1)
   {
      var popup_options2 = { top: 25 };
      
      var touts2    = new Array();
      var cur_elem2 = null;
      var n2=0;
      $("#mainmenu > li").each(function () {
         if ( !$(this).children("div").length )
            return;
         $(this).attr("n", ++n2).addClass("parent");
      });
      
      $("#mainmenu > li.parent").hover(function () {
         cur_elem2 = $("div", this);
         touts2[ parseInt($(this).attr("n")) ] = setTimeout(function () {
            if ($.browser.msie)
            {
               cur_elem2.show().css({
                  display:    'block',
                  top:        popup_options2.top+"px"
               });
            }
            else
            {
               cur_elem2.css({
                  opacity:    0,
                  display:    'block',
                  top: ( popup_options2.top + popup_options.jump_height )+"px"
               }).animate({
                  opacity:    1,
                  top:        popup_options2.top+"px"
               }, {
                  duration:   popup_options.show_duration,
                  queue:      false
               });
            }
         }, popup_options.tout);
      }, function () {
      
         if (touts2[ parseInt($(this).attr("n")) ]) clearTimeout(touts2[ parseInt($(this).attr("n")) ]);
      
         if ($.browser.msie)
         {
            $("div", this).hide();
         }
         else
         {
            $("div", this).animate({
               opacity:    0
            }, {
               duration:   popup_options.hide_duration,
               queue:      false,
               complete:   function () {
                  $(this).hide();
               }
            });
         }   
      });
   }
*/   
      
jQuery(function(){
	$("#mainmenu li").hover(function () {
												 
		  $(this).children('div').css({visibility: "visible",display: "none"}).fadeIn('normal');
	  }, function () {
		  
		  $('div', this).css({visibility: "hidden"});
	  });
});
jQuery(function() {
	$('#mainmenu li div ul li div').each( function() {
		$(this).parent('li').addClass('droup');
	})
})


/* Some deprecated stuff from PrettyPhoto */

/*

   $(".gal").attr("rel", "gal[g]");
   if ($.prettyPhoto && $(".gal").length)
   {
      $(".gal").each(function () {
         $(this).attr("rel", "gal[g]")
            .attr("title",  $(this).find("h4").text() );
      });
      $("a[rel=gal\\[g\\]]").prettyPhoto({
         theme: 'light_rounded',
         gallery_markup: ''
      });
   }
*/

   $(".sh").each(function () {
      var now = 0;
      var maxnow = $(this).children(".item").length-1;
      var ee = $(this);
      $(this).parent().find(".larr, .rarr").click(function () {
         var the_now = now;
         if ( !$(this).hasClass('larr') ) now++; else now--;
         if (now<0) now = maxnow;
         if (now>maxnow) now=0;
         var now_h = ee.height();
         //$(".widget_arr").hide();
         ee.find(".item:eq("+the_now+")").fadeOut(300, function () {
            var gg = ee.find(".item:eq("+now+")");
            gg.show();
            ee.css({ height: 'auto' });
            var new_h = ee.height();
            gg.hide();
            ee.css({ height: now_h }).animate({ height: new_h }, { duration: 300, complete: function () {
               //$(".widget_arr").show();
            } });
            gg.fadeIn(300);
         });
         return false;
      });
   });

    // highslide options
    hs.showCredits = false;
    
    if( 'photos' == DT_vars.PageType ) {
    
        // highslide attach
        hs.align = 'center';
        hs.transitions = ['expand', 'crossfade'];
        hs.fadeInOut = true;
        hs.dimmingOpacity = 0.8;
        hs.wrapperClassName = 'borderless floating-caption';
        hs.marginLeft = 100; // make room for the thumbstrip
        hs.marginBottom = 80 // make room for the controls and the floating caption
        
        // Add the simple close button
        hs.registerOverlay({
            html: '<div class="closebutton" onclick="return hs.close(this)" title="Close"></div>',
            position: 'top right',
            fade: 2 // fading the semi-transparent overlay looks bad in IE
        });
        
        // Add the slideshow providing the controlbar and the thumbstrip
        hs.addSlideshow({
            //slideshowGroup: 'group1',
            interval: 5000,
            repeat: true,
            useControls: true,
            overlayOptions: {
                className: 'text-controls',
                position: 'bottom center',
                relativeTo: 'viewport',
                offsetX: 50,
                offsetY: -5
            }
        });
        
    }else if( 'gallery' == DT_vars.PageType || 'default' == DT_vars.PageType ) {

        // highslide attach
        hs.align = 'center';
        hs.transitions = ['expand', 'crossfade'];
        hs.fadeInOut = true;
        hs.dimmingOpacity = 0.8;
        hs.wrapperClassName = 'borderless floating-caption';
        //hs.captionEval = 'this.thumb.alt';
        hs.marginLeft = 100; // make room for the thumbstrip
        hs.marginBottom = 80 // make room for the controls and the floating caption
        hs.numberPosition = 'caption';
        hs.lang.number = '%1/%2';
        
        // Add the simple close button
        hs.registerOverlay({
            html: '<div class="closebutton" onclick="return hs.close(this)" title="Close"></div>',
            position: 'top right',
            fade: 2 // fading the semi-transparent overlay looks bad in IE
        });
        
        // Add the slideshow providing the controlbar and the thumbstrip
        hs.addSlideshow({
            //slideshowGroup: 'group1',
            interval: 5000,
            repeat: true,
            useControls: true,
            overlayOptions: {
                className: 'text-controls',
                position: 'bottom center',
                relativeTo: 'viewport',
                offsetX: 50,
                offsetY: -5
            },

            thumbstrip: {
                position: 'middle left',	
                mode: 'vertical',
                relativeTo: 'viewport'
            }
        });

        
        $("#content").find(".cols.benefits").click(function(event) {

            if( $(event.target).parents(".highslide-gallery").is("div") ) {
                
				var gall_holder = $(event.target).parents(".highslide-gallery").find('> .hidden-container > a'),
					check_attr = gall_holder.eq(0).attr('data-src');

				if (typeof check_attr !== 'undefined' && check_attr !== false) {
	                gall_holder.each(function() {
	                    var href = $(this).attr('href'),
	                    	src = $(this).attr('data-src'),
	                    	caption = $(this).text(),
	                    	hendler = $(this).attr('onclick'),
	                    	new_html = '<a class="highslide" href="'+ href +'" title="'+ caption +'" onclick="' + hendler + '"><img src="'+ src +'" alt="' + caption + '" /></a>';

	                    $(this).replaceWith(new_html);
	                });
				}

                var container = $(event.target).parents(".highslide-gallery");
                var gallery = container.find('.hidden-container > a');
                var album_cover = container.find("a[class*='dt_album_cover']");
                var slideshow_group = album_cover.attr('id').slice(4);
                                
                if( gallery.length > 0 ) {
					event.preventDefault();
                    return hs.expand( gallery[0], { slideshowGroup: slideshow_group } );
                }
                
                if( $(event.target).is('input[type=submit]') ) {
                    event.preventDefault();
                    $(event.target).parent('form').submit();
                }
                
                return false;
            }
        });
        

    }else {
        // highslide attach
        hs.align = 'center';
        hs.transitions = ['expand', 'crossfade'];
        hs.fadeInOut = true;
        hs.dimmingOpacity = 0.8;
        hs.wrapperClassName = 'borderless floating-caption';
        //hs.captionEval = 'this.thumb.alt';
        hs.marginLeft = 100; // make room for the thumbstrip
        hs.marginBottom = 80 // make room for the controls and the floating caption
        hs.numberPosition = 'caption';
//        hs.lang.number = '%1/%2';
        
        // Add the simple close button
        hs.registerOverlay({
            html: '<div class="closebutton" onclick="return hs.close(this)" title="Close"></div>',
            position: 'top right',
            fade: 2 // fading the semi-transparent overlay looks bad in IE
        });
        
        // Add the slideshow providing the controlbar and the thumbstrip
        hs.addSlideshow({
            //slideshowGroup: 'group1',
            interval: 5000,
            repeat: true,
            useControls: true,
            overlayOptions: {
                className: 'text-controls',
                position: 'bottom center',
                relativeTo: 'viewport',
                offsetX: 50,
                offsetY: -5
            },

            thumbstrip: {
                position: 'middle left',	
                mode: 'vertical',
                relativeTo: 'viewport'
            }
        });
        
        // Thumbstrip Formatter
        hs.stripItemFormatter = function(a) {
            var img = a.getElementsByTagName('img');
            return '<img src="' + img[0].src + '" alt=""/>';
        }

    }
    
    $('.gall_std').click(function(event) {
        if( $(event.target).is('span') ) {
            $(event.target).prev('a').click();
        }
    });
    // photos highslide end
});


jQuery(function() {
	$('.toggle:first').addClass('first')
})
jQuery(function() {
	$('#content .widget_b:last').addClass('last');
	$('.spread_20:last').addClass('last');
})

//footer
$(function () {
	$(window).resize(function () {
		h = $(window).height() - $("#header_bg").height() - $("#pxs_container").height() - $("#breadcrumbs_bg").height() - $("#bottom").height() - $("#footer_bg").height();
		if ($("#footer_bg").height() > 0) {
			h = h - 35;
		}
		if (h > 0) {
			$("#content_spot_b").css({'min-height': h });
		} else {
			$("#content_spot_b").css({'min-height': 'auto' });
		}
	});
	$(window).trigger("resize");
});
jQuery(function() {	
	$(".frame-img.alignleft").each(function () {
		$(this).wrap('<div class="frame position-left" />');		
		var i=$(this)
		var i_w = i.attr("width");	
		$(this).parent('.frame').css({width: i_w });
	})
	$(".frame-img.alignright").each(function () {
		$(this).wrap('<div class="frame position-right" />');
		var i=$(this)
		var i_w = i.attr("width");
		$(this).parent('.frame').css({width: i_w });
	})
	$(".frame-img.aligncenter").each(function () {
		$(this).wrap('<div class="frame position-center" />');
		var i=$(this)
		var i_w = i.attr("width");
		$(this).parent('.frame').css({width: i_w });
	})
	$(".frame-img.alignnone").each(function () {
		$(this).wrap('<div class="frame position-none" />');
		var i=$(this)
		var i_w = i.attr("width");
		$('.frame').css({width: i_w });
	})

});

function showBlackLoader() {
	$("#content").css({'min-height':'180px', 'position':'relative'});
	var blackLoader = $('<div id="black_loader"><span /></div>').appendTo(".cols.benefits");
}

jQuery(function() {
	dt_photos_fade( 'a.blog_ico, a.news_ico, ul.gallery:not(.to_attachment) li, .col_1-3.highslide' );	
})